//
//  CarManager.m
//  citycar
//
//  Created by Tang JiaQi on 3/30/11.
//  Copyright 2011 HOME. All rights reserved.
//

#import "CarManager.h"

#import "Car.h"

@implementation CarManager

@synthesize aiCars;
@synthesize userCars;

static CarManager* _sharedCarManager = nil;

+(CarManager*) sharedCarManager
{
	if (!_sharedCarManager) {
		_sharedCarManager = [[self alloc] init];
	}
	return _sharedCarManager;
}

-(id) init
{
	if((self == [super init]))
	{
		userCars = [[NSMutableArray alloc] init];
		aiCars = [[NSMutableArray alloc] init];
		
		[self schedule:@selector(updateCars:)];
	}
	return self;
}

-(void) updateCars: (ccTime)dt
{
	for(unsigned int i = 0; i < [aiCars count]; ++i)
	{
		[[aiCars objectAtIndex:i] updateCar: dt];
	}
	for(unsigned int i = 0; i < [userCars count]; ++i)
	{
		[[userCars objectAtIndex:i] updateCar: dt];
	}
}

-(Car*) getUserCar
{
	int numberofUserCars = [userCars count];
	if(numberofUserCars > 0)
		return [userCars objectAtIndex:0];
	return nil;
}

-(Car*) addCar: (CarRole) role carType: (CarType) type initPos: (CGPoint)pos initVelocity: (TsiU::Vec2)vel initOrientation: (TsiU::Vec2)ori
{
	Car* newCar = [Car createNewCar: type];
	if(!newCar)
		return nil;
	
	newCar.velocity		= vel;
	newCar.position		= pos;
	newCar.orientation	= ori;
	
	if(role == ECarRole_User)
	{
		[userCars addObject: newCar];
	}
	else
	{
		[aiCars addObject: newCar]; 
	}
	return newCar;
}

@end
